//
//  HomeMapVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/11/17.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "HomeMapVC.h"
#import <MapKit/MapKit.h>
#import "HomeAnnotation.h"
#import "HomeAnnotationView.h"
#import "HomeMapCustomView.h"

@interface HomeMapVC ()<MKMapViewDelegate>

@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) UIButton *backBtn;

@end

@implementation HomeMapVC

-(UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, StatusBarHeight + 7, 44, 44)];
        [_backBtn setImage:[UIImage imageNamed:@"icon_map_back"] forState:0];
        [_backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (void)backClick {
    [self.rt_navigationController popViewControllerAnimated:YES];
}

-(MKMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        _mapView.delegate = self;
    }
    return _mapView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    [self.view addSubview:self.mapView];
    [self.view addSubview:self.backBtn];
    NSMutableArray *quYuArray = [NSMutableArray array];
    [self.model.location enumerateObjectsUsingBlock:^(HomeYunShuMapDotModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        HomeAnnotation *annotation = [HomeAnnotation new];
        annotation.coordinate = CLLocationCoordinate2DMake([obj.lat doubleValue], [obj.lng doubleValue]);
        annotation.wa_id = obj.wa_id;
        annotation.wa_num = obj.wa_num;
        annotation.goodsNum = obj.goodsNum;
        annotation.goodsJnum = obj.goodsJnum;
        annotation.shippers_name = obj.shippers_name;
        [self.mapView addAnnotation:annotation];
        [quYuArray addObject:annotation];
    }];
    if (!quYuArray.count) {
        [self.mapView removeAnnotations:[self.mapView annotations]];
    }else {
        self.mapView.region = [HomeAnnotation regionForAnnotations:quYuArray];
    }
}

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
    // 自定义大头针
    if ([annotation isKindOfClass:[HomeAnnotation class]]) {
        static NSString *pinID = @"HomeAnnotation";
        HomeAnnotationView *customPinView = (HomeAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
        if (!customPinView) {
           customPinView = [[HomeAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID];
        }
//        customPinView.canShowCallout = YES;
        customPinView.annotation = (HomeAnnotation *)annotation;

//        HomeMapCustomView *view = [HomeMapCustomView new];
//        view.model = (HomeAnnotation *)annotation;
//        customPinView.detailCalloutAccessoryView = view;
        return customPinView;
    }
    return nil;
}

//- (UIView *)detailViewForAnnotation:(HomeAnnotation *)annotation {
//
//UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
//    view.backgroundColor = [UIColor redColor];
//view.translatesAutoresizingMaskIntoConstraints = false;
//    view.intrinsicContentSize = CGSizeMake(200, 100);
//
//UILabel *label = [[UILabel alloc] init];
//
//label.text = @"ceshi";
//
//label.font = [UIFont systemFontOfSize:20];
//
//label.translatesAutoresizingMaskIntoConstraints = false;
//
//label.numberOfLines = 0;
//
//[view addSubview:label];
//
//UIButton *button = [UIButton new];
//[button setImage:[UIImage imageNamed:@"icon_home_transport"] forState:0];
//[view addSubview:button];
//
//NSDictionary *views = NSDictionaryOfVariableBindings(label, button);
//
//[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label]|" options:0 metrics:nil views:views]];
//
//[view addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
//
//[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label]-[button]|" options:0 metrics:nil views:views]];
//
//return view;
//
//}

//-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
//
//}
//
//-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
//
//}

@end
